package com.showgood.util;

import java.awt.BorderLayout;
import java.awt.Font;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class AppConsole extends JFrame {
	private static final long serialVersionUID = -556390325785711567L;
	
	private JTextArea textarea;
	private Process process;
	
	public AppConsole() {
		textarea = new JTextArea();
		this.getContentPane().setLayout(new BorderLayout());
		this.getContentPane().add(new JScrollPane(textarea));
		textarea.setFont(new Font("Consolas", Font.PLAIN, 12));
	}

	public static void main(String[] args) {
		AppConsole win = new AppConsole();
		win.setBounds(100, 100, 800, 600);
		win.setVisible(true);
		List<String> argList = Arrays.asList(args);
		win.traceProcessOutput(argList);
	}

	private void traceProcessOutput(List<String> args) {
		ProcessBuilder pb = new ProcessBuilder(args);
		try {
			process = pb.start();
			InputStream is = process.getInputStream();
			byte[] buff = new byte[2048];
			int len;
			while ((len = is.read(buff)) >= 0) {
				textarea.append(new String(buff, 0, len));
			}
			textarea.append("\nprogram termated\n");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
